﻿using System;
using XD.Office;
using System.IO;

namespace TestOffice
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("~~~~~~~~~~~~~~~~~~~~测试Microsoft Office~~~~~~~~~~~~~~~~~~~~");
            Console.WriteLine();
            string version;
            string path;
            if (InspectionOfficeInstall.IsInstallOffice(out version, out path) == false)
            {
                Console.WriteLine("未安装Office！");
            }
            else
            {
                Console.WriteLine("Office 版本：" + version);
                Console.WriteLine("Office 安装路径：" + path);
                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~测试Microsoft Excel~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine();
                if (InspectionOfficeInstall.IsInstallExcel() == false)
                {
                    Console.WriteLine("未安装Excel！");
                }
                else
                {
                    string excelTemplate = Path.Combine(Environment.CurrentDirectory, "Template", "excel.xls");
                    string excelPath = @"C:\testExcel_" + DateTime.Now.Ticks.ToString() + ".xls";
                    try
                    {
                        File.Copy(excelTemplate, excelPath, true);
                        Console.WriteLine("测试Excel的文件路径为：" + excelPath);
                        try
                        {
                            using (ExcelHelper excel = new ExcelHelper(excelPath, EnumXlMode.exOpen))
                            {
                                excel.SetCurrentWorkSheet(1);
                                excel.SetCellValue(1, 9, "test");

                                excel.Save();
                            }

                            Console.WriteLine("Excel可正常使用！");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Excel异常：" + ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Excel模版拷贝失败：" + ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

                Console.WriteLine();

                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~测试Microsoft Word~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine();
                if (InspectionOfficeInstall.IsInstallWord() == false)
                {
                    Console.WriteLine("未安装Word！");
                }
                else
                {
                    string wordTemplate = Path.Combine(Environment.CurrentDirectory, "Template", "word.doc");
                    string wordPath = @"C:\testWord_" + DateTime.Now.Ticks.ToString() + ".doc";
                    try
                    {
                        File.Copy(wordTemplate, wordPath, true);
                        Console.WriteLine("测试Word的文件路径为：" + wordPath);
                        try
                        {
                            using (WordHelper word = new WordHelper(wordPath, EnumWdMode.wdOpen))
                            {
                                word.InsertTable(3, 3);
                                word.FillTableCell(1, 1, 1, "test");

                                word.Save();
                            }

                            Console.WriteLine("Word可正常使用！");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Word异常：" + ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Word模版拷贝失败：" + ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

                Console.WriteLine();

                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~测试Microsoft Access~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine();
                if (InspectionOfficeInstall.IsInstallAccess() == false)
                {
                    Console.WriteLine("未安装Access！");
                }
                else
                {
                    string accessTemplate = Path.Combine(Environment.CurrentDirectory, "Template", "access.mdb");
                    string accessPath = @"C:\testAccess_" + DateTime.Now.Ticks.ToString() + ".mdb";
                    try
                    {
                        File.Copy(accessTemplate, accessPath, true);
                        Console.WriteLine("测试Access的文件路径为：" + accessPath);
                        try
                        {
                            using (AccessHelper dataManage = new AccessHelper(accessPath))
                            {
                                dataManage.Open();

                                dataManage.Close();
                            }

                            Console.WriteLine("Access可正常使用！");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Access异常：" + ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Access模版拷贝失败：" + ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
                Console.WriteLine();
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            }

            Console.Read();
        }
    }
}
